package com.example.administrator.printoutindemo.fragment;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.dds.soap.SoapUtil;
import com.example.administrator.printoutindemo.R;
import com.example.administrator.printoutindemo.base.BaseFragment;
import com.example.administrator.printoutindemo.http.SPUtil;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.utils.ToastUtil;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class SenqingpeisongFragment extends BaseFragment {
    private View view;
    private TextView tv_riqi, tv_chexing, tv_tijiao, tv_maohao, tv_maohao1, tv_fenge, et_sqr;
    private EditText tv_kehu, tv_phone, et_zysjd, et_rengong, et_rwms, et_yusuan, et_shiji, et_cllxr, et_lxrdianhua, et_fapiao, et_beizhu;
    private int year, monthOfYear, dayOfMonth;
    private AlertDialog alertDialog; //单选框
    private EditText et_zysjd1, et_zysjd2, et_zysjd3;
    private String zytime = "";
    private RadioButton rb_yes, rb_no;
    private RadioGroup rg_check;
    private String fap = "";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_sqps, null);
        tv_maohao = view.findViewById(R.id.tv_maohao);
        tv_maohao1 = view.findViewById(R.id.tv_maohao1);
        tv_fenge = view.findViewById(R.id.tv_fenge);
        et_zysjd1 = view.findViewById(R.id.et_zysjd1);
        et_zysjd2 = view.findViewById(R.id.et_zysjd2);
        et_zysjd3 = view.findViewById(R.id.et_zysjd3);
        tv_riqi = view.findViewById(R.id.tv_riqi);
        tv_chexing = view.findViewById(R.id.tv_chexing);
        tv_kehu = view.findViewById(R.id.tv_kehu);
        tv_phone = view.findViewById(R.id.tv_phone);
        tv_tijiao = view.findViewById(R.id.tv_tijiao);
        et_zysjd = view.findViewById(R.id.et_zysjd);
        et_rengong = view.findViewById(R.id.et_rengong);
        et_rwms = view.findViewById(R.id.et_rwms);
        et_yusuan = view.findViewById(R.id.et_yusuan);
        et_shiji = view.findViewById(R.id.et_shiji);
        et_sqr = view.findViewById(R.id.et_sqr);
        et_cllxr = view.findViewById(R.id.et_cllxr);
        et_lxrdianhua = view.findViewById(R.id.et_lxrdianhua);
        et_beizhu = view.findViewById(R.id.et_beizhu);
        rg_check = view.findViewById(R.id.rg_check);
        rb_yes = view.findViewById(R.id.rb_yes);
        rb_no = view.findViewById(R.id.rb_no);
        tv_chexing.setText("4.2米货车");
        et_sqr.setText(SPUtil.getString(getActivity(), "username", ""));
        //选择车型
        tv_chexing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String[] items = {"4.2米货车", "9.6米货车", "13米货车", "3吨叉车", "16吨吊车", "50吨吊车", "暂无信息"};
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
                alertBuilder.setTitle("车型");
                alertBuilder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(getActivity(), items[i], Toast.LENGTH_SHORT).show();
                        tv_chexing.setText(items[i]);
                    }
                });
                alertBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        alertDialog.dismiss();
                    }
                });

                alertBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        alertDialog.dismiss();
                    }
                });

                alertDialog = alertBuilder.create();
                alertDialog.show();
            }
        });
        //获取系统时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String times = format.format(Calendar.getInstance().getTime());
        Log.e("shifenmiao============", times);
        tv_riqi.setText(times);
        rg_check.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {
                    case R.id.rb_yes:
                        fap = "有";
                        break;
                    case R.id.rb_no:
                        fap = "无";
                        break;
                }
            }
        });
        tv_tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (!tv_phone.getText().toString().equals("") & !tv_chexing.getText().toString().equals("") & !et_rengong.getText().toString().equals("")
                        & !et_rwms.getText().toString().equals("") & !et_yusuan.getText().toString().equals("") & !tv_kehu.getText().toString().equals("") & !et_cllxr.getText().toString().equals("")
                        & !et_lxrdianhua.getText().toString().equals("") & !fap.equals("") & !et_zysjd.getText().toString().equals("") & !et_zysjd1.getText().toString().equals("")
                        & !et_zysjd2.getText().toString().equals("") & !et_zysjd3.getText().toString().equals("")) {

                    zytime = et_zysjd.getText().toString() + tv_maohao.getText().toString() + et_zysjd1.getText().toString() + tv_fenge.getText().toString() + et_zysjd2.getText().toString() + tv_maohao1.getText().toString() + et_zysjd3.getText().toString();

                    SoapParams params = new SoapParams();

                    params.put("userLogin", SPUtil.getString(getContext(), "userlogin", ""));
                    params.put("applyDate", tv_riqi.getText().toString());
                    params.put("workPeriod", zytime);
                    params.put("models", tv_chexing.getText().toString());
                    params.put("artificialNums", et_rengong.getText().toString());
                    params.put("taskDescription", et_rwms.getText().toString());
                    params.put("budget", et_yusuan.getText().toString());
                    params.put("actualCost", "0.00");
                    params.put("applicant", et_sqr.getText().toString());
                    params.put("customer", tv_kehu.getText().toString());
                    params.put("customerTel", tv_phone.getText().toString());
                    params.put("vehicleContacts", et_cllxr.getText().toString());
                    params.put("vehicleContactsTel", et_lxrdianhua.getText().toString());
                    params.put("receipt", fap);
                    params.put("remarks", et_beizhu.getText().toString());
                    addDelivery(params);

                } else {
                    Toast.makeText(getActivity(), "请填写必填项！", Toast.LENGTH_LONG).show();
                }

            }
        });

        return view;
    }

    private void addDelivery(SoapParams params) {


        String methodName = "AddDelivery";

        SoapUtil soapUtil = SoapUtil.getInstance(getActivity());
        soapUtil.setDotNet(true);

        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    int a = Integer.parseInt(result);
                    if (a > 0) {
                        ToastUtil.showMessage(getActivity(), "提交成功！");
                        tv_chexing.setText("");
                        tv_kehu.setText("");
                        tv_phone.setText("");
                        et_rengong.setText("");
                        et_rwms.setText("");
                        et_yusuan.setText("");
                        et_shiji.setText("");
                        et_sqr.setText("");
                        et_cllxr.setText("");
                        et_lxrdianhua.setText("");
                        et_beizhu.setText("");
                    }

                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
            }
        }, getAapiUserName(), getApiPwd());


    }
}
